import { CustomTableInterface } from '@/components/ProTable/interface';
/**
 * @name 文件上传
 */

import { PublicInterface } from '@/interface/public';
import { useFileForm } from './use-fileForm';
import { InterfaceFile } from '@/api/interface/basic/file';
import { FileDataProps } from '../index.vue';
import { useFile } from './use-file';

export function useFileData(fileRef: PublicInterface.ProTableValue, props: FileDataProps) {
  const { addFileForm } = useFileForm();
  const { fileObj, verificationFormat, onChange, fileBeforeUpload, onSuccess, onExceed } = useFile(addFileForm, fileRef);

  /**
   * @name 图片,pdf 预览
   */
  let viewer = ref({
    visible: false,
    pics: '',
    type: 'image'
  });

  let fileData = reactive<CustomTableInterface>({
    transitionArr: [
      {
        oldField: 'ehrFileType',
        newField: 'type'
      }
    ],
    initParam: {},
    // 表格配置项
    columns: [
      { prop: 'operation', label: 'operation', fixed: 'left', width: 200 },
      {
        prop: 'type',
        label: '文件类型',
        isShow: false,
        enum: [],
        search: {
          el: 'select',
          span: 2,
          changeMethod: () => {
            fileRef.value?.search();
          }
        }
      },
      {
        prop: 'fileName',
        label: '文件名称'
      },
      {
        prop: 'remark',
        label: 'remark'
      }
    ],
    headButtonList: [
      {
        label: '附件上传',
        type: 'primary',
        icon: 'el-Plus',
        permissionCode: props.upLoadingPermissionCode,
        methods: () => {
          formOpen(addFileForm, '附件上传');
          fileObj.value.fileList = [];
        }
      }
    ],
    rowButtonList: [
      {
        label: '预览',
        type: 'primary',
        display: row => verificationFormat(row, 'imgSuffix') || verificationFormat(row, 'pdfSuffix'),
        permissionCode: props.previewPermissionCode,
        methods: (row: InterfaceFile.ResFileList) => {
          const type = verificationFormat(row, 'imgSuffix') ? 'image' : 'pdf';
          openPreview(row, type);
        },
        attrs: {
          link: true
        }
      },
      {
        label: '下载',
        type: 'primary',
        permissionCode: props.downloadPermissionCode,
        methods: (row: InterfaceFile.ResFileList) => {
          const params = { id: row.id };
          filePreview(params).then(data => {
            exportData(data, row.fileName, true);
          });
        },
        attrs: {
          link: true
        }
      },
      {
        label: '删除',
        type: 'danger',
        permissionCode: props.deletePermissionCode,
        methods: (row: InterfaceFile.ResFileList) => {
          const params = { id: row.id };
          useHandleData(fileDelete, params, `删除【${row.fileName}】`, fileRef.value?.getTableList);
        },
        attrs: {
          link: true
        }
      }
    ]
  });

  console.log(fileData, 'fileData');

  const openPreview = (row: InterfaceFile.ResFileList, fileType: PublicInterface.PublicType['filePreviewType']) => {
    Object.assign(viewer.value, {
      visible: true,
      pics: `/basic/file/v1/preview/${row.id}`,
      type: fileType
    });
  };

  const changeSearchParams = params => {
    params.type = `${params.type}&${props.dictionariesCode}`;
    return params;
  };
  return { fileData, changeSearchParams, addFileForm, viewer, fileObj, onChange, fileBeforeUpload, onSuccess, onExceed };
}
